package hadix.parse

import hadix.Info
import hadix.Tag
import org.apache.commons.lang.StringUtils
import org.jsoup.Jsoup

/**
 * User: hadix
 * Date: 13-7-14
 * Time: AM10:26
 */
class ParserShoeshr extends Parser {
    String searchPageLink
    def pageFetcher

    @Override
    protected Info doParse(Info info, String html) {
        def doc = Jsoup.parse(html)
        def tds = doc.select("table.job_table td")
        info.salary = tds[9].text()
        info.sex = tds[17].text()
        info.desc = doc.select('div.job_need').text()
        info.con_name = tds[25].text()
        info.con_phone = tds[29].text()
        info.con_addr = tds[35].text()
        info.con_email = tds[33].text()
        if (info.company.link) {
            String page = pageFetcher.call(info.company.link)
            doc = Jsoup.parse(page)
            info.company.desc = doc.select('div.company_intro').first().ownText()
            tds = doc.select('table.job_table td')
            info.company.scale = tds[5].ownText()
            info.company.addr = tds[17].ownText()
        }
        info
    }

    @Override
    List<Tag> parseTypeLinks(Closure<String> contentProvider) {
        return [new Tag('main_link', searchPageLink)]
    }

    @Override
    protected List<Info> doParseInfos(String html) {
        def doc = Jsoup.parse(html)
        doc.select('table.j_list_table tr:gt(0)').collect { tr ->
            def tds = tr.select('td')
            def info = Info.newInstance()
            def title_a = tds[1].select('a').first()
            info.title = title_a.attr('title')
            info.link = title_a.attr('href')
            info.id = StringUtils.substringBetween(info.link, '/', '.html')
            def company_a = tds[2].select('a').first()
            info.company.name = company_a.attr('title')
            info.company.link = "http://www.shoeshr.com${company_a.attr('href')}"
            info.work_area = tds[3].ownText()
            info.edu = tds[4].ownText()
            info.exp = tds[5].ownText()
            info.count = tds[6].ownText()
            info.time = tds[7].ownText()
            info
        }
    }

    @Override
    protected String pageLink(String baseLink, Integer nextPageNo) {
        "${baseLink}&page=${nextPageNo}"
    }
}
